package br.edu.udc.sistemas.ia5.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import br.edu.udc.sistemas.ia5.database.DatabasePool;
import br.edu.udc.sistemas.ia5.entity.Entity;
import br.edu.udc.sistemas.ia5.factory.Factory;
import br.edu.udc.sistemas.ia5.query.Query;

public class Dao {

	private Connection connection;

	public Dao() throws Exception {
		this.connection = DatabasePool.getInstance().getConnection();
	}

	public void commit() throws Exception {
		if ((this.connection != null) && (!this.connection.isClosed())) {
			this.connection.commit();
		}
	}

	public void rollback() throws Exception {
		if ((this.connection != null) && (!this.connection.isClosed())) {
			this.connection.rollback();
		}
	}

	public void save(Entity obj) throws Exception {
		try {
			if ((obj.getIdValue() == null) || (obj.getIdValue().equals(""))
					|| (obj.getIdValue().equals("null"))) {
				String sql = Query.getSQLInsert(obj);
				System.out.println(sql);
				Statement stmt = this.connection.createStatement();
				stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
				ResultSet rst = stmt.getGeneratedKeys();
				if (rst.next()) {
					obj.setIdValue(rst.getString(1));
				}
			} else {
				String sql = Query.getSQLUpdate(obj);
				System.out.println(sql);
				Statement stmt = this.connection.createStatement();
				stmt.executeUpdate(sql);
			}
		} catch (Exception e) {
			this.rollback();
			throw e;
		}

		// try {
		// if ((Reflection.getIdField(obj.getClass()).get(obj) == null) ||
		// (Reflection.getIdField(obj.getClass()).get(obj).equals("")) ||
		// (Reflection.getIdField(obj.getClass()).get(obj).equals("null"))) {
		// String sql = Query.getSQLInsert(obj);
		// System.out.println(sql);
		// Statement stmt = this.connection.createStatement();
		// stmt.executeUpdate(sql,Statement.RETURN_GENERATED_KEYS);
		// ResultSet rst = stmt.getGeneratedKeys();
		// if (rst.next()) {
		// Reflection.getIdField(obj.getClass()).set(obj,rst.getInt(1));
		// }
		// } else {
		// String sql = Query.getSQLUpdate(obj);
		// System.out.println(sql);
		// Statement stmt = this.connection.createStatement();
		// stmt.executeUpdate(sql);
		// }
		// } catch (Exception e) {
		// this.rollback();
		// throw e;
		// }
	}

	public void delete(Entity obj) throws Exception {
		try {
			String sql = Query.getSQLDelete(obj);
			System.out.println(sql);
			Statement stmt = this.connection.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}

	public Entity[] find(Entity obj) throws Exception {
		try {
			String sql = Query.getSQLSelect(obj);
			System.out.println(sql);
			Statement stmt = this.connection.createStatement();
			ResultSet rst = stmt.executeQuery(sql);
			return Factory.createByResultSet(obj, rst);
		} catch (Exception e) {
			this.rollback();
			throw e;
		}
	}

	public Entity findByPrimaryKey(Entity obj) throws Exception {
		if ((obj.getIdValue() != null) && (!obj.getIdValue().equals(""))
				&& (!obj.getIdValue().equals("null"))) {
			Entity objAux = obj.getClass().newInstance();
			objAux.setIdValue(obj.getIdValue());
			Entity result[] = this.find(objAux);
			if (result.length > 0) {
				return result[0];
			}
		}
		return null;
		//
		// if ((Reflection.getIdField(obj.getClass()).get(obj) != null) &&
		// (!Reflection.getIdField(obj.getClass()).get(obj).equals("")) &&
		// (!Reflection.getIdField(obj.getClass()).get(obj).equals("null"))) {
		// Object objAux = obj.getClass().newInstance();
		// // objAux.setIdValue(obj.getIdValue());
		// Reflection.getIdField(objAux.getClass()).set(objAux,Reflection.getIdField(obj.getClass()).get(obj));
		// Object result[] = this.find(objAux);
		// if (result.length > 0) {
		// return result[0];
		// }
		// }
		// return null;
		// }
	}
}